package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbParentFactoryInfo;
import com.sailfish.springbootdemo.pojo.db2.PcbSonFactoryInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbParentFactoryInfoDao {

    @Results(id = "pcbParentFactoryInfoResultMap", value = {
            @Result(column = "pcb_parent_factory_id", property = "pcbParentFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "company_name", property = "companyName", jdbcType = JdbcType.INTEGER),
            @Result(column = "company_name_en", property = "companyNameEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_website", property = "companyWebsite", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_name_full", property = "companyNameFull", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_code", property = "companyCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_name", property = "factoryName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_name_en", property = "factoryNameEn", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_introduction", property = "factoryIntroduction", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_introduction_en", property = "factoryIntroductionEn", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_contacts", property = "factoryContacts", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_phone", property = "factoryPhone", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_email", property = "factoryEmail", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_category", property = "factoryCategory", jdbcType = JdbcType.VARCHAR),
            @Result(column = "factory_category_en", property = "factoryCategoryEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark_pic", property = "remarkPic", jdbcType = JdbcType.VARCHAR),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select * " +
            " from " + DB2TableName.PCB_PARENT_FACTORY +
            " where pcb_parent_factory_id = #{id}" +
            "</script>")
    PcbParentFactoryInfo getById(@Param("id") Integer id);

    /*
     *查询单个
     */
    @Select("<script> " +
            "select * " +
            " from " + DB2TableName.PCB_PARENT_FACTORY +
            " where status = 1" +
            "</script>")
    List<String> getFactoryName();

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select pcb_parent_factory_id, company_name, company_name_en,company_name_full,company_code, company_website, factory_name, factory_name_en, factory_introduction, factory_introduction_en, factory_contacts, factory_phone, factory_email, factory_category, factory_category_en, create_time, update_time, status, sort, remark_pic " +
            "from " + DB2TableName.PCB_PARENT_FACTORY +
            " where 1=1 " +
            "<if test='companyCode != null  and companyCode != \"\"'>" +
            " and (company_code like concat('%', #{companyCode},'%'))" +
            "</if>" +
            "<if test='companyName != null and companyName != \"\" ' >" +
            " and (company_name like concat('%', #{companyName},'%'))" +
            "</if>" +
            "<if test='companyNameFull != null and companyNameFull != \"\" ' >" +
            " and (company_name_full like concat('%', #{companyNameFull},'%'))" +
            "</if>" +
            "<if test='companyNameEn != null and companyNameEn != \"\"'>" +
            " and (company_name_en like concat('%',#{companyNameEn},'%'))" +
            "</if>" +
            "<if test='companyWebsite != null and companyWebsite != \"\"'>" +
            " and (company_website like concat('%',#{companyWebsite},'%'))" +
            "</if>" +
            "<if test='factoryName != null and factoryName != \"\"'>" +
            " and (factory_name = #{factoryName})" +
            "</if>" +
            "<if test='factoryNameEn != null and factoryNameEn != \"\"'>" +
            " and (factory_name_en = #{factoryNameEn})" +
            "</if>" +
            "<if test='factoryIntroduction != null and factoryIntroduction != \"\"'>" +
            " and (factory_introduction = #{factoryIntroduction})" +
            "</if>" +
            "<if test='factoryIntroductionEn != null and factoryIntroductionEn != \"\"'>" +
            " and (factory_introduction_en = #{factoryIntroductionEn})" +
            "</if>" +
            "<if test='factoryContacts != null and factoryContacts != \"\"'>" +
            " and (factory_contacts = #{factoryContacts})" +
            "</if>" +
            "<if test='factoryCategory != null and factoryCategory != \"\"'>" +
            " and (factory_category = #{factoryCategory})" +
            "</if>" +
            "<if test='factoryCategoryEn != null and factoryCategoryEn != \"\"'>" +
            " and (factory_category_en = #{factoryCategoryEn})" +
            "</if>" +
            "<if test='status != null'>" +
            " and (status = #{status})" +
            "</if>" +
            " order by sort" +
            "</script>")

    @ResultMap(value = "pcbParentFactoryInfoResultMap")
    List<PcbParentFactoryInfo> getAllByLimit(PcbParentFactoryInfo pcbParentFactoryInfo);

    /*
     *模糊搜索
     */
    @Select("<script> " +
            "select pcb_parent_factory_id, company_name, company_name_en,company_name_full,company_code, company_website, factory_name, factory_name_en, factory_introduction, factory_introduction_en, factory_contacts, factory_phone, factory_email, factory_category, factory_category_en, create_time, update_time, status, sort , remark_pic " +
            "from " + DB2TableName.PCB_PARENT_FACTORY +
            " where 1=1 " +
            "<if test='searchName != null  and searchName != \"\"'>" +
            " and (company_name like concat('%', #{searchName},'%') " +
            "or company_name_en like concat('%', #{searchName},'%')" +
            "or company_name_full like concat('%', #{searchName},'%')" +
            "or company_code like concat('%', #{searchName},'%')" +
            "or company_website like concat('%', #{searchName},'%'))" +
            "</if>" +
//            " and (status = 1)" +
            " order by sort" +
            "</script>")

    @ResultMap(value = "pcbParentFactoryInfoResultMap")
    List<PcbParentFactoryInfo> getAll(@Param("searchName") String searchName);
    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_PARENT_FACTORY +
            " where 1=1 " +
            "<if test='pcbParentFactoryId != null'>" +
            " and (pcb_parent_factory_id = #{pcbParentFactoryId})" +
            "</if>" +
            "<if test='companyCode != null  and companyCode != \"\"'>" +
            " and (company_code = #{companyCode})" +
            "</if>" +
            "<if test='companyName != null and companyName != \"\" ' >" +
            " and (company_name = #{companyName})" +
            "</if>" +
            "<if test='companyNameEn != null and companyNameEn != \"\"'>" +
            " and (company_name_en = #{companyNameEn}))" +
            "</if>" +
            "<if test='companyWebsite != null and companyWebsite != \"\"'>" +
            " and (company_website = #{companyWebsite})" +
            "</if>" +
            "<if test='factoryName != null and factoryName != \"\"'>" +
            " and (factory_name = #{factoryName})" +
            "</if>" +
            "<if test='factoryNameEn != null and factoryNameEn != \"\"'>" +
            " and (factory_name_en = #{factoryNameEn})" +
            "</if>" +
            "<if test='factoryIntroduction != null and factoryIntroduction != \"\"'>" +
            " and (factory_introduction = #{factoryIntroduction})" +
            "</if>" +
            "<if test='factoryIntroductionEn != null and factoryIntroductionEn != \"\"'>" +
            " and (factory_introduction_en = #{factoryIntroductionEn})" +
            "</if>" +
            "<if test='factoryContacts != null and factoryContacts != \"\"'>" +
            " and (factory_contacts = #{factoryContacts})" +
            "</if>" +
            "<if test='factoryCategory != null and factoryCategory != \"\"'>" +
            " and (factory_category = #{factoryCategory})" +
            "</if>" +
            "<if test='factoryCategoryEn != null and factoryCategoryEn != \"\"'>" +
            " and (factory_category_en = #{factoryCategoryEn})" +
            "</if>" +
            "<if test='status != null'>" +
            " and (status = #{status})" +
            "</if>" +
            " order by sort" +
            "</script>")
    Integer getAllCount(Integer pcbParentFactoryId, String companyName, String companyNameEn, String companyWebsite, String factoryName,
                        String factoryNameEn, String factoryIntroduction, String factoryIntroductionEn,
                        String factoryContacts, String factoryCategory, String factoryCategoryEn, Integer status);

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_PARENT_FACTORY + " ( company_name, company_name_en, company_name_full, company_code, company_website, factory_name, factory_name_en, factory_introduction, factory_introduction_en, factory_contacts, factory_phone, factory_email, factory_category, factory_category_en, create_time, update_time, status, sort, remark_pic) values " +
            "( #{companyName}, #{companyNameEn}, #{companyNameFull},#{companyCode}, #{companyWebsite}, #{factoryName}, #{factoryNameEn}, #{factoryIntroduction}, #{factoryIntroductionEn}, #{factoryContacts}, #{factoryPhone}, #{factoryEmail}, #{factoryCategory}, #{factoryCategoryEn}, #{createTime}, #{updateTime}, #{status}, #{sort}, #{remarkPic})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbSonFactoryId")
    Integer insert(PcbParentFactoryInfo param);

    /*
     *批量新增
     */
    @Insert("insert into " + DB2TableName.PCB_PARENT_FACTORY + " ( company_name, company_name_en, company_name_full, company_code, company_website, factory_name, factory_name_en, factory_introduction, factory_introduction_en, factory_contacts, factory_phone, factory_email, factory_category, factory_category_en, create_time, update_time, status, sort, remark_pic) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(# #{entity.companyName}, #{entity.companyNameEn},  #{entity.companyNameFull},#{entity.companyCode},#{entity.companyWebsite}, #{entity.factoryName}, #{entity.factoryNameEn}, #{entity.factoryIntroduction}, #{entity.factoryIntroductionEn}, #{entity.factoryContacts}, #{entity.factoryPhone}, #{entity.factoryEmail}, #{entity.factoryCategory}, #{entity.factoryCategoryEn}, #{entity.createTime}, #{entity.updateTime}, #{entity.status}, #{entity.sort}, #{entity.remarkPic})")
    Integer insertBatch(@Param("entities") List<PcbParentFactoryInfo> entities);


    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_PARENT_FACTORY + " where pcb_parent_factory_id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("delete from " + DB2TableName.PCB_PARENT_FACTORY + " where  pcb_parent_factory_id in " +
            "<foreach collection=\"ids\" item = \"id\" open=\"(\" separator=\",\" close=\")\">  " +
            "#{id} " +
            "</foreach>"
    )
    Integer deleteBatch(@Param("ids") List<Integer> ids);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_PARENT_FACTORY +
            "<set>" +
            "<if test='companyName != null and companyName != \"\"'>" +
            "company_name = #{companyName}," +
            "</if>" +
            "<if test='companyNameEn != null '>" +//and companyNameEn != ""
            "company_name_en = #{companyNameEn}," +
            "</if>" +
            "<if test='companyNameFull != null and companyNameFull != \"\"'>" +
            "company_name_full = #{companyNameFull}," +
            "</if>" +
            "<if test='companyCode != null and companyCode != \"\"'>" +
            "company_code = #{companyCode}," +
            "</if>" +
            "<if test='companyWebsite != null '>" +//and companyWebsite != ""
            "company_website = #{companyWebsite}," +
            "</if>" +
            "<if test='factoryName != null '>" +//and factoryName != ""
            "factory_name = #{factoryName}," +
            "</if>" +
            "<if test='factoryNameEn != null '>" +//and factoryNameEn != ""
            "factory_name_en = #{factoryNameEn}," +
            "</if>" +
            "<if test='factoryIntroduction != null '>" +//and factoryIntroduction != ""
            "factory_introduction = #{factoryIntroduction}," +
            "</if>" +
            "<if test='factoryIntroductionEn != null '>" +//and factoryIntroductionEn != ""
            "factory_introduction_en = #{factoryIntroductionEn}," +
            "</if>" +
            "<if test='factoryContacts != null '>" +//and factoryContacts != ""
            "factory_contacts = #{factoryContacts}," +
            "</if>" +
            "<if test='factoryPhone != null '>" +//and factoryPhone != ""
            "factory_phone = #{factoryPhone}," +
            "</if>" +
            "<if test='factoryEmail != null '>" +//and  factoryEmail != ""
            "factory_email = #{factoryEmail}," +
            "</if>" +
            "<if test='factoryCategory != null '>" +//and factoryCategory != ""
            "factory_category = #{factoryCategory}," +
            "</if>" +
            "<if test='factoryCategoryEn != null '>" +//and factoryCategoryEn != ""
            "factory_category_en = #{factoryCategoryEn}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='status != null'>" +
            "status = #{status}," +
            "</if>" +
            "<if test='sort != null'>" +
            "sort = #{sort}, " +
            "</if>" +
            "<if test='remarkPic != null'>" +
            "remark_pic = #{remarkPic} " +
            "</if>" +
            "</set>" +
            "where pcb_parent_factory_id = #{pcbParentFactoryId}" +
            "</script>")
    Integer update(PcbParentFactoryInfo param);
}
